package com.mingmen.canting.gongyingshang;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mingmen.canting.R;
import com.mingmen.canting.adapter.CommentAdapter;
import com.mingmen.canting.adapter.MyCommentAdapter;
import com.mingmen.canting.adapter.PhotoWaiAdapter;
import com.mingmen.canting.defineview.PhotoDialog;
import com.mingmen.canting.fragment.BaseFragment;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.model.CommentModel;
import com.mingmen.canting.model.MySimpleData;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

@SuppressLint("ValidFragment")
public class CommentFragment extends BaseFragment {


    @BindView(R.id.recycle_comment)
    RecyclerView recycleComment;
    Unbinder unbinder;
    private String mTitle;

    private RecyclerView vRecycler;


    public static CommentFragment getInstance(String title) {
        CommentFragment sf = new CommentFragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    RecyclerView recycle_comment;

    List<String> reData = new ArrayList<>();
    MyCommentAdapter myCommentAdapter;
    private String img1 = "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3933355596,669391437&fm=26&gp=0.jpg";
    private String img2 = "http://img4.imgtn.bdimg.com/it/u=1445674920,12445384&fm=26&gp=0.jpg";
    private String img3 = "http://img4.imgtn.bdimg.com/it/u=482593725,2192787331&fm=26&gp=0.jpg";


    private String img10 = "http://img3.imgtn.bdimg.com/it/u=2911588266,3715444639&fm=26&gp=0.jpg";
    private String img11 = "http://img2.imgtn.bdimg.com/it/u=1823088812,4234717774&fm=26&gp=0.jpg";
    private String img12 = "http://img0.imgtn.bdimg.com/it/u=1196506210,3510975282&fm=26&gp=0.jpg";
//    private String img13 = "http://fms.news.cn/swf/2019_qmtt/10_8_2019_qm_z/images/5.jpg";

    private ArrayList<String> imageData = new ArrayList<>();
    private List<MySimpleData> datas = new ArrayList<>();
    //    private PhotoWaiAdapter adapter;
    private CommentAdapter adapter;
    String token;
    List<CommentModel.DataBean.DetailsBean> commentDataList = new ArrayList<>();
    @BindView(R.id.qualityScore)
    TextView qualityScore;
    @BindView(R.id.packagingScore)
    TextView packagingScore;
    @BindView(R.id.satisfaction)
    TextView satisfaction;
    @BindView(R.id.score)
    TextView score;
    @BindView(R.id.shop_Score)
    ImageView shop_Score;
    @BindView(R.id.pictureNumber)
    TextView pictureNumber;
    @BindView(R.id.negativeNumber)
    TextView negativeNumber;
    String supplierId;
    @Override
    public View initView() {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.fr_comment, null);
        unbinder = ButterKnife.bind(this, v);
        recycle_comment = v.findViewById(R.id.recycle_comment);
        token = (String) SharePreferenceUtil.get(getActivity(), "token", "");
        supplierId= (String) SharePreferenceUtil.get(getActivity(), "supplierId", "");
        recycle_comment.setLayoutManager(new LinearLayoutManager(getActivity()));
        myCommentAdapter = new MyCommentAdapter(getActivity(), reData);
        for (int i = 0; i < 10; i++) {
            reData.add("ITem" + i);
        }
//        recycle_comment.setAdapter(myCommentAdapter);
//        myCommentAdapter.setDataList(reData);
        adapter = new CommentAdapter(getActivity(), commentDataList);
        recycle_comment.setAdapter(adapter);
        adapter.setCallBack(new CommentAdapter.OnMyAdapterCallBack() {
            @Override
            public void onItemClick(int position, List<String> itemImgs) {
                Bundle bundle = new Bundle();
                ArrayList<String> data = new ArrayList<>();
                data.addAll(itemImgs);
                bundle.putInt("currentPostion", position);
                bundle.putStringArrayList("imageData", (ArrayList<String>) itemImgs);

                PhotoDialog photoDialog = new PhotoDialog();
                photoDialog.setArguments(bundle);
                photoDialog.show(getActivity().getSupportFragmentManager(), "");
            }
        });
        initData();
        HttpResponse.getSupplierEvaluate(token,supplierId , new StringCallback(getActivity()) {
            @Override
            public void onSuccess(String s, Call call, Response response) {

                Log.d("getSupplierEvaluate", s);
                CommentModel commentModel = new CommentModel();
                commentModel = GsonUtil.GsonToBean(s, CommentModel.class);
                commentDataList.addAll(commentModel.getData().get(0).getDetails());
                adapter.setData(commentDataList);

                DecimalFormat format = new DecimalFormat("");

                qualityScore.setText(commentModel.getData().get(0).getQualityScore());


                packagingScore.setText(commentModel.getData().get(0).getPackagingScore());
                satisfaction.setText(commentModel.getData().get(0).getSatisfaction());
                score.setText(commentModel.getData().get(0).getScore());
                pictureNumber.setText("有图  (" + commentModel.getData().get(0).getPictureNumber() + ")");

                negativeNumber.setText("差评 " + commentModel.getData().get(0).getNegativeNumber() + "");
                BigDecimal bigDecimal = new BigDecimal(commentModel.getData().get(0).getScore());
                BigDecimal bigDecimal0 = new BigDecimal("10");
                BigDecimal bigDecima20 = new BigDecimal("20");
                BigDecimal bigDecima30 = new BigDecimal("30");
                BigDecimal bigDecima40 = new BigDecimal("40");
                BigDecimal bigDecima50 = new BigDecimal("50");
                if (bigDecimal.compareTo(bigDecimal0) == 0) {
//10
                    shop_Score.setImageResource(R.mipmap.star_first);
                } else if (bigDecimal.compareTo(bigDecimal0) == -1) {
                    shop_Score.setImageResource(R.mipmap.star_first);
                } else if (bigDecimal.compareTo(bigDecimal0) == 1 && bigDecimal.compareTo(bigDecima20) == -1) {
                    shop_Score.setImageResource(R.mipmap.star_second);
                } else if (bigDecimal.compareTo(bigDecima20) == 0) {
//10
                    shop_Score.setImageResource(R.mipmap.star_first);
                } else if (bigDecimal.compareTo(bigDecima20) == 1 && bigDecimal.compareTo(bigDecima30) == -1) {
                    shop_Score.setImageResource(R.mipmap.star_three);
                } else if (bigDecimal.compareTo(bigDecima30) == 0) {
//10
                    shop_Score.setImageResource(R.mipmap.star_three);
                } else if (bigDecimal.compareTo(bigDecima30) == 1 && bigDecimal.compareTo(bigDecima40) == -1) {
                    shop_Score.setImageResource(R.mipmap.star_forth);
                } else if (bigDecimal.compareTo(bigDecima40) == 0) {
//10
                    shop_Score.setImageResource(R.mipmap.star_forth);
                } else if (bigDecimal.compareTo(bigDecima40) == 1 && bigDecimal.compareTo(bigDecima50) == -1) {
                    shop_Score.setImageResource(R.mipmap.star_fifth);
                } else if (bigDecimal.compareTo(bigDecima50) == 0) {
//10
                    shop_Score.setImageResource(R.mipmap.star_fifth);
                }
            }
        });
        initListener();
        return v;
    }


    @Override
    public void initData() {
        MySimpleData data = new MySimpleData();
        data.setTitle("秒速5厘米");
        imageData.add(img1);
        imageData.add(img2);
        imageData.add(img3);
     /*   imageData.add(img4);
        imageData.add(img5);
        imageData.add(img6);
        imageData.add(img7);
        imageData.add(img8);
        imageData.add(img9);*/
        data.setImgs(imageData);

        datas.add(0, data);

        MySimpleData data2 = new MySimpleData();
        data2.setTitle("EVA");
        ArrayList<String> childData = new ArrayList<>();
        childData.add(img10);
        childData.add(img11);
        childData.add(img12);
//        childData.add(img13);
        data2.setImgs(childData);

        datas.add(1, data2);
    }


    private void initListener() {


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.qualityScore, R.id.recycle_comment})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.qualityScore:
                break;
            case R.id.recycle_comment:
                break;
        }
    }
}